package com.kailu.hotel.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.kailu.hotel.model.RoomModel;
import com.kailu.hotel.service.RoomManager;

/**
 * @author viet.ch
 * @generated Apr 22, 2014
 * @from VN
 */

@Controller
public class RoomController {

	@Autowired
	RoomManager roomManager;
	
	@RequestMapping( value = "/roommanager", method = RequestMethod.GET)
	public String getAllRoom(ModelMap map){
		map.addAttribute("roomList",roomManager.getAllRoom());
		return "roommanager";
	}
	
	@RequestMapping( value = "/roommanager/addnew", method = RequestMethod.GET)
	public String addNew(ModelMap map){
		map.addAttribute("room", new RoomModel());
		return "roommanager_add";
	}

	@RequestMapping(value = "/roommanager/update/{room_id}", method = RequestMethod.GET)
	public String getRoom(@PathVariable("room_id") int room_id,
			ModelMap map) {
		map.addAttribute("room", roomManager.getRoom(room_id));
		return "roommanager_update";
	}
	
	@RequestMapping(value = "/roommanager/add", method = RequestMethod.POST)
    public String addRoom(@ModelAttribute(value="room") RoomModel room, BindingResult result)
    {
        roomManager.addRoom(room);
        return "redirect:/roommanager";
    }
	
	@RequestMapping(value = "/roommanager/update/takeupdate", method = RequestMethod.POST)
    public String updateRoom(@ModelAttribute(value="room") RoomModel room, BindingResult result)
    {
        roomManager.updateRoom(room);
        return "redirect:/roommanager";
    }
	
	@RequestMapping(value = "/roommanager", method = RequestMethod.POST)
    public String searchRoom(@ModelAttribute(value="search") String search,ModelMap map)
    {
		if (!search.isEmpty()){
			map.addAttribute("roomList", roomManager.searchRoom(search));
		} else {
			map.addAttribute("roomList",roomManager.getAllRoom());
		}
        return "roommanager";
    }
	
	@RequestMapping(value = "/roommanager/delete/{room_id}", method = RequestMethod.GET)
	public String deleteRoom(@PathVariable("room_id") int room_id) {
		roomManager.deleteRoom(room_id);
		return "redirect:/roommanager";
	}
}


